package com.then.recruitment;

import android.app.Application;


import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.then.recruitment.utils.NetWorkUtils;

import java.io.File;

/**
 * Created by wk on 15/3/10.
 */
public class MainApp extends Application {
    public RequestQueue requestQueue = null;
    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader();
        initVollery();
        initRequestContext();

    }
    //初始化请求context对象
    private void initRequestContext() {
        Request.requestContext = this;
        Request.token = NetWorkUtils.getToken(this);
    }

    //初始化请求框架
    private void initVollery() {
        requestQueue = Volley.newRequestQueue(this);
    }

    //初始化图片加载
    private void initImageLoader() {
        File cacheDir = StorageUtils.getCacheDirectory(this);
        cacheDir = new File(cacheDir,"image_cache");
        ImageLoaderConfiguration imageLoaderConfiguration =  new ImageLoaderConfiguration.Builder(this)
                .memoryCache(new LruMemoryCache(10))
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .threadPriority(Thread.MIN_PRIORITY)
                .build();
        ImageLoader imageLoader  = ImageLoader.getInstance();
        imageLoader.init(imageLoaderConfiguration);
    }

    //当内存比较低得时候 清除内存中的图片
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        ImageLoader.getInstance().clearMemoryCache();
    }
}
